package com.cl.ks.flow;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.ks.entity.SparkNode;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GJ3KS {
    // GJ流程图转换成KS流程图：前端展示用的JSON转换
    public static void main(String[] args) throws IOException {
        JSONObject ksGraph = new JSONObject();
        ksGraph.put("description", "");
        JSONObject chartData = new JSONObject();
        // 节点信息
        JSONArray ksNodes = new JSONArray();
        // 端点信息
        JSONArray endpoints = new JSONArray();
        // 连线信息
        JSONArray edges = new JSONArray();
        // 模板默认的开始和结束节点
        ksNodes.add(JSONObject.parseObject("{\"id\":\"start\",\"points\":{\"targets\":[],\"sources\":[\"start\"]},\"position\":{\"left\":611,\"top\":51},\"data\":{\"value\":\"开始\",\"icon\":\"el-icon-s-home\"}}"));
        ksNodes.add(JSONObject.parseObject("{\"id\":\"end\",\"points\":{\"targets\":[\"end\"],\"sources\":[]},\"position\":{\"left\":604,\"top\":1060},\"data\":{\"value\":\"结束\",\"icon\":\"el-icon-switch-button\"}}"));


        // 从文件读取需要转换的JSON
        String gj = FileUtils.readFileToString(new File("./gj.json"), StandardCharsets.UTF_8);
        JSONObject gjObject = JSONObject.parseObject(gj);
        JSONArray gjNodes = gjObject.getJSONArray("nodes");
        JSONArray gjLinks = gjObject.getJSONArray("links");

        // 构建to和from的映射
        Map<String, List<String>> toAndFrom = new HashMap<>();
        for (int i = 0; i < gjLinks.size(); i++) {
            JSONObject link = gjLinks.getJSONObject(i);
            String from = (String) link.get("from");
            String to = (String) link.get("to");
            toAndFrom.putIfAbsent(to, new ArrayList<>());
            toAndFrom.get(to).add(from);
        }
        // 遍历需要转换的节点
        for (int i = 0; i < gjNodes.size(); i++) {
            JSONObject gjNode = gjNodes.getJSONObject(i);
            String nodeid = gjNode.getString("nodeid");
            String name = gjNode.getString("name");
            String type = gjNode.getString("type");

            // 构建ksNode
            JSONObject ksNode = new JSONObject();
            ksNode.put("id", nodeid);
            // 构建ksNode的points
            JSONObject ksNodePoints = new JSONObject();
            JSONArray ksNodePointsTargets = new JSONArray();
            JSONArray ksNodePointsSources = new JSONArray();
            // 构建ksNode的position
            JSONObject gjPosition = gjNode.getJSONObject("style");
            String gjPositionX = gjPosition.getString("x");
            String gjPositionY = gjPosition.getString("y");
            JSONObject ksNodePosition = new JSONObject();
            ksNodePosition.put("left", gjPositionX);
            ksNodePosition.put("top", gjPositionY);
            ksNode.put("position", ksNodePosition);
            // 构建该节点输出端点
            JSONObject sourceEndpoint = new JSONObject();
            String sourceNodeid = "source-" + nodeid;
            JSONObject sourceEndpointType = new JSONObject();
            sourceEndpointType.put("value", "输出");
            sourceEndpoint.put("id", sourceNodeid);
            sourceEndpoint.put("data", sourceEndpointType);
            endpoints.add(sourceEndpoint);
            // 构建ksNode的points
            ksNodePointsSources.add(sourceNodeid);
            ksNodePoints.put("sources", ksNodePointsSources);
            // 构建该节点输入端点
            JSONObject targetEndpointType = new JSONObject();
            targetEndpointType.put("value", "输入");
            if (gjNode.containsKey("properties")) {
                JSONObject properties = gjNode.getJSONObject("properties");

                // 构建ksNode的data字段
                JSONObject data = new JSONObject();
                JSONObject dataNode = new JSONObject();
                if ("DATAELEMENT".equals(type)) {
                    String tablename = properties.getString("tablename");
                    dataNode.put("targetTable", tablename);
                }
                data.put("icon", "el-icon-arrow-right");
                data.put("value", name);
                data.put("node", dataNode);
                ksNode.put("data", data);
                // 判断输入端点是否为两个
                if (properties.containsKey("leftnodeid") && properties.containsKey("rightnodeid")) {
                    // 合并或连接等需要两个输入的节点，需要构建两个端点
                    JSONObject leftEndpoint = new JSONObject();
                    JSONObject rightEndpoint = new JSONObject();
                    String leftNodeid = "target-left-" + nodeid;
                    String rightNodeid = "target-right-" + nodeid;

                    leftEndpoint.put("id", leftNodeid);
                    leftEndpoint.put("data", targetEndpointType);
                    rightEndpoint.put("id", rightNodeid);
                    rightEndpoint.put("data", targetEndpointType);

                    endpoints.add(leftEndpoint);
                    endpoints.add(rightEndpoint);
                    // 构建ksNode的points
                    ksNodePointsTargets.add(leftNodeid);
                    ksNodePointsTargets.add(rightNodeid);

                    // 构建连线
                    String leftnodeid = properties.getString("leftnodeid");
                    String rightnodeid = properties.getString("rightnodeid");
                    String leftEdge = "source-" + leftnodeid + "&&" + leftNodeid;
                    String rightEdge = "source-" + rightnodeid + "&&" + rightNodeid;

                    edges.add(leftEdge);
                    edges.add(rightEdge);
                } else {
                    // 单个输入的节点，构建一个端点
                    JSONObject targetEndpoint = new JSONObject();
                    String targetNodeid = "target-" + nodeid;

                    targetEndpoint.put("id", targetNodeid);
                    targetEndpoint.put("data", targetEndpointType);

                    endpoints.add(targetEndpoint);
                    // 构建ksNode的points
                    ksNodePointsTargets.add(targetNodeid);

                    if (toAndFrom.containsKey(nodeid)) {
                        String edge = "source-" + toAndFrom.get(nodeid).get(0) + "&&" + targetNodeid;
                        edges.add(edge);
                    }

                }
            }
            ksNodePoints.put("targets", ksNodePointsTargets);

            ksNode.put("points", ksNodePoints);
            ksNodes.add(ksNode);
        }

        chartData.put("nodes", ksNodes);
        chartData.put("endpoints", endpoints);
        chartData.put("edges", edges);
        ksGraph.put("chartData", chartData);

        String s = JSONObject.toJSONString(ksGraph, SerializerFeature.DisableCircularReferenceDetect);

        System.out.println();
    }
}
